<template>
    <div class="app-table">
        <div class="app-body">
            <div class="app-search">
                <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="起止时间">
                        <el-config-provider :locale="locale">
                            <el-date-picker v-model="form.duration" type="daterange" range-separator="To"
                                start-placeholder="开始时间" end-placeholder="结束时间" value-format="YYYY-MM-DD"
                                :size="large" />
                        </el-config-provider>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit" @keyup.enter="searchEnter">搜索</el-button>
                        <el-button type="success" @click="calculation('')">统计考勤</el-button>
                        <el-button type="warning" @click="getSummaryData">下载汇总数据</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table size="small" :data="list" border :row-class-name="tableRowClassName">
                <el-table-column prop="id" label="序号" />
                <el-table-column prop="createDate" label="考勤日期" />
                <el-table-column prop="total" label="总人数" />
                <el-table-column prop="qualifiedNumber" label="合格人数" />
                <el-table-column prop="rate" label="合格率" />
                <el-table-column label="操作" width="300px">
                    <template style="display: block" #default="scope">
                        <el-button size="small" type="primary" @click="getDayData(scope.row.createDate)">下载数据
                        </el-button>
                        <el-button size="small" type="success" @click="calculation(scope.row.createDate)">重新计算
                        </el-button>
                        <el-button v-if="scope.row.id == 1" size="small" type="danger"
                            @click="deleteTeacherSignInStatistics(scope.row.createDate)">删除
                        </el-button>
                    </template>
                </el-table-column>
                <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <div style="margin-top: 10px">
                <el-config-provider :locale="locale">
                    <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                        layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                        @current-change="changeCurrentPage">
                    </el-pagination>
                </el-config-provider>
            </div>
        </div>
        <bottomView></bottomView>
    </div>
</template>
<script>
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
    name: "TeacherSignInStatisticsListView",
    data() {
        return {
            type: 0,
            title: "",
            page: 1,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            form: {
                duration: "",
            },
        };
    },
    components: {
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        bottomView,
        ElMessage, Upload
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getTeacherSignInStatisticsList();
    },
    methods: {
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getTeacherSignInStatisticsList();
            }
        },
        calculation(createDate) {
            ElMessage({
                message: '数据计算中，请稍后查看',
                type: 'success',
            });
            httpService(
                "/pythonApi/attendanceApi/recalculation",
                {
                    'createDate': createDate
                },
                "get"
            ).then((data) => {
                if (data.code == 200) {
                    // ElMessage({
                    //     message: '计算完成',
                    //     type: 'success',
                    // });
                    this.getTeacherSignInStatisticsList()
                }
            });
        },
        deleteTeacherSignInStatistics(createDate) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/pythonApi/attendanceApi/deleteSignData", "createDate=" + createDate, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getTeacherSignInStatisticsList();
                            } else {
                                ElMessage.error("删除失败，请重试！");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getTeacherSignInStatisticsList()
        },
        exportExcel(data, fileName) {
            let link = document.createElement('a'), fileUrl = window.URL.createObjectURL(new Blob([data]));
            link.style.display = 'none';
            link.href = fileUrl;
            link.setAttribute('download', fileName + '.xlsx');
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            window.URL.revokeObjectURL(fileUrl)
        },
        onSubmit() {
            this.getTeacherSignInStatisticsList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getTeacherSignInStatisticsList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getTeacherSignInStatisticsList();
        },
        getTeacherSignInStatisticsList() {
            if (this.form.duration == null) {
                this.form.duration = ""
            };
            httpService(
                "/pythonApi/attendanceApi/getSignData",
                {
                    page: this.page,
                    limit: this.limit,
                    duration: this.form.duration,
                    type: 1
                },
                "get"
            ).then((data) => {
                this.list = data.data;
                this.total = data.total;
                this.pageCount = data.pages;
            });
        },
        getDayData(createDate) {
            let config = { "responseType": "blob" };
            httpService("/pythonApi/attendanceApi/getFileData",
                {
                    createDate: createDate,
                    type: 1
                }, 'get', config).then(
                    (data) => {
                        let fileName = createDate.toString();
                        this.exportExcel(data, fileName)
                    }
                )
        },
        getSummaryData() {
            let config = { "responseType": "blob" }, duration = this.form.duration;
            if (duration) {
                httpService("/pythonApi/attendanceApi/getSummaryData",
                    {
                        duration: duration,
                        type: 2
                    },
                    'get', config).then(
                        (data) => {
                            let fileName = duration.toString();
                            this.exportExcel(data, fileName)
                        }
                    )
            } else {
                alert('请选择时间段')
            }
        }
    },
};
</script>
<style scoped>
.app-table {
    background: #eff3f5;
    padding: 10px;
    width: calc(100vw - 193px);
}

.app-search {}


.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 243px);
}
</style>
